<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading"  :rules="rules">
      <el-form-item label="题目编号：" prop="analyze" >
        <el-input v-model="form.id"  disabled v-if="form.id" style="width: 224px;"/>
      </el-form-item>
      <el-form-item label="大学科：" prop="subjectParentId" required>
        <el-select v-model="form.subjectParentId" placeholder="学科"  @change="mainSubjectChange01">
          <el-option v-for="item in mainSubject" :key="item[0]" :value="item[0]" :label="item[1]"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="小学科："  prop="subjectId" required >
        <el-select v-model="form.subjectId" placeholder="小学科" @change="mainSubjectChange02" clearable>
          <!-- 内层循环遍历对应主学科下的小学科列表，这里使用template标签包裹来进行正确的循环逻辑 -->
          <template v-for="mainItem in mainSubject01">
            <template v-if="mainItem[0]===form.subjectParentId">
              <el-option v-for="subItem in mainItem[2]" :key="subItem[0]" :value="subItem[0]" :label="subItem[1]">
              </el-option>
            </template>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="题型：" prop="questionTypeId" required>
        <el-select v-model="form.questionTypeId" placeholder="题型">
            <el-option v-for="item in filteredQuestionType"  :key="item[0]" :disabled="item[2]!==form.subjectParentId" :value="item[0]" :label="item[1]">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="章节：">
        <el-select v-model="form.chapterId" placeholder="章节">
          <el-option v-for="item in filteredChapter" :disabled="item.parentId!==form.subjectId" :key="item.id" :value="item.id" :label="item.categoryName">
          </el-option>
        </el-select>
      </el-form-item>
      <single-choice v-if="form.questionTypeId===25 || form.questionTypeId===26 || form.questionTypeId===27 || form.questionTypeId===29" :message="form"></single-choice>
      <short-answer v-if="form.questionTypeId===28" :message="form"></short-answer>
      <reading-comprehension v-if="form.questionTypeId===20 || form.questionTypeId===40" :message="form"></reading-comprehension>
      <gap-filling v-if="form.questionTypeId===22" :message="form"></gap-filling>
      <translation-question v-if="form.questionTypeId===21 || form.questionTypeId===23 || form.questionTypeId===24" :message="form"></translation-question>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import questionApi from '@/api/question'
import singleChoice from '@/views/exam/question/edit/single-choice.vue'
import shortAnswer from '@/views/exam/question/edit/short-answer.vue'
import readingComprehension from '@/views/exam/question/edit/reading-comprehension.vue'
import gapFilling from '@/views/exam/question/edit/gap-filling.vue'
import translationQuestion from '@/views/exam/question/edit/translation_question.vue'

export default {
  components: {
    singleChoice,
    shortAnswer,
    readingComprehension,
    gapFilling,
    translationQuestion
  },
  data () {
    return {
      form: {
        id: null,
        questionTypeId: null,
        questionTypeName: '',
        subjectId: null,
        gradeLevel: null,
        subjectParentId: null,
        chapterName: '',
        chapterId: null,
        subjectName: '',
        title: '',
        textContent: [
          {
            qtitle: '',
            item: [
              {
                prefix: '',
                content: ''
              }
            ]
          }
        ],
        status: null,
        articleId: null,
        articleContent: '',
        createUserId: null,
        createUserName: '',
        correct: '',
        createTime: '',
        correctArray: [],
        score: null,
        difficult: null,
        modifyTime: '',
        suggestTime: null,
        picture: ''
      },
      subjectFilter: null,
      formLoading: false,
      rules: {
        subjectParentId: [
          { required: true, message: '请选择大学科', trigger: 'change' }
        ],
        questionTypeId: [
          { required: true, message: '请选择题型', trigger: 'change' }
        ],
        subjectId: [
          { required: true, message: '请选择小学科', trigger: 'change' }
        ]
      },
      richEditor: {
        dialogVisible: false,
        object: null,
        parameterName: '',
        instance: null
      },
      questionShow: {
        qType: 0,
        dialog: false,
        question: null,
        loading: false
      },
      allQuestionType: [],
      mainSubject: [],
      allQuestionType01: [],
      mainSubject01: [],
      chapter: [
        {
          id: null,
          categoryName: '',
          parentId: null
        }
      ]
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    this.initSubject(function () {
      _this.subjectFilter = _this.subjects
    })
    if (id !== null && id !== '' && id !== undefined) {
      _this.formLoading = true
      questionApi.select(id).then(re => {
        _this.form = re.response
        _this.form.textContent = JSON.parse(re.response.textContent)
        _this.formLoading = false
      })
    }
    this.typeChange()
    this.createdAllChapter()
  },
  computed: {
    filteredQuestionType () {
      return this.allQuestionType.filter(item => item[2] === this.form.subjectParentId)
    },
    filteredChapter () {
      return this.chapter.filter(item => item.parentId === this.form.subjectId)
    }
  },
  methods: {
    createdAllChapter () {
      let _this = this
      questionApi.selectAllChapter().then(re => {
        _this.chapter = re.response
        this.chapter = re.response
      })
    },
    typeChange () {
      this.listLoading = true
      questionApi.selectType().then(data => {
        if (data.code === 1) {
          const re = data.response[0]
          this.mainSubject = re.mainSubject
          this.allQuestionType = re.allQuestionType
          this.allQuestionType01 = re.allQuestionType
          this.mainSubject01 = re.mainSubject
        } else {
          console.error('获取数据失败，原因：', data.message)
        }
        this.listLoading = false
      }
      )
    },
    mainSubjectChange02 () {
      // 这里编写当主科目选项改变时需要执行的逻辑，例如加载对应主科目的相关题型等
      console.log('小科目选项发生改变，当前选中的值为：', this.form.subjectId)
    },
    mainSubjectChange01 () {
      // 这里编写当主科目选项改变时需要执行的逻辑，例如加载对应主科目的相关题型等
      console.log('主科目选项发生改变，当前选中的值为：', this.form.subjectParentId)
      this.form.subjectId = null
      this.form.questionTypeId = null
      this.allQuestionType = this.allQuestionType01
      this.allQuestionType = this.allQuestionType.filter(data => data[2] === this.form.subjectParentId)
      this.mainSubject01 = this.mainSubject.filter(data => data[0] === this.form.subjectParentId)
    },
    ...mapActions('exam', { initSubject: 'initSubject' }),
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  }
}
</script>
